package com.huifer.wgx.controller;

import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.utlis.DateUtil;
import com.huifer.wgx.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.sql.Timestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : TaskController </p>
 * <p>Description : 任务</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("task")
@Api(tags = "TaskController", description = "任务管理")
public class TaskController {

    @Autowired
    private TaskService taskService;
    private CommonResult commonResult = new CommonResult();


    @ApiOperation("修改任务(特指任务接受)")
    @PutMapping("/edit_do")
    public ResponseEntity taskStart(
            @RequestParam("assign_name") String assignName,
            @RequestParam("start_time") String startTime,
            @RequestParam("do_name") String doName
    ) {
        try {
            Timestamp timesp = DateUtil.getTimestamp(DateUtil.getDateymd(startTime));
            return ResponseEntity
                    .ok(commonResult.success(taskService.updateDoTaks(assignName, timesp, doName)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改任务(特指任务结束)")
    @PutMapping("/edit_end")
    public ResponseEntity taskEnd(
            @RequestParam("pagenum") String assignName,
            @RequestParam("pagesize") String startTime,
            @RequestParam("pagesize") String doName,
            @RequestParam("pagesize") String endTime
    ) {
        try {
            Timestamp start = DateUtil.getTimestamp(DateUtil.getDateymd(startTime));
            Timestamp end = DateUtil.getTimestamp(DateUtil.getDateymd(endTime));
            return ResponseEntity.ok(commonResult
                    .success(taskService.updateEndTask(assignName, start, doName, end)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("创建任务")
    @PostMapping("/save")
    public ResponseEntity saveTask(
            @RequestParam("title") String title,
            @RequestParam("note") String note,
            @RequestParam("assign_name") String assignName
    ) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(taskService.saveTask(title, note, assignName)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("查询所有任务")
    @GetMapping("/find_all")
    public ResponseEntity findAllTask(
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity.ok(commonResult.success(taskService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

}
